package com.zlm.app.partner.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zlm.app.partner.service.UserPartnerService;
import com.zlm.common.base.BaseController;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.entity.UserTokenDO;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: duyufu
 * @Date: 2019/5/10 16:16
 */
public class CheckController extends BaseController {

    @Autowired
    private UserPartnerService userPartnerService;

    /**
     * 检查token
     * @param token
     */
    public UserTokenDO checkToken(String token) {
        //判断token失效
        JsonResultDto result = userPartnerService.getLoginPartner(token);
        if (null == result || null == result.getData()) {
            throw new RuntimeException(CodeConstants.CODE_101);
        }
        if (!CodeConstants.CODE_SUCCESS.equals(result.getCode())) {
            throw new RuntimeException(result.getCode());
        }
        ObjectMapper mapper = new ObjectMapper();
        UserTokenDO userToken = mapper.convertValue(result.getData(), UserTokenDO.class);
        return userToken;
    }

    /**
     * 检查服务返回
     * @param result
     */
    public void checkJsonResultDto(JsonResultDto result) {
        if (null == result) {
            throw new RuntimeException(CodeConstants.CODE_BUSY);
        }
        if (!CodeConstants.CODE_SUCCESS.equals(result.getCode())) {
            throw new RuntimeException(result.getCode());
        }
    }



}
